/**
 * 
 */
package com.et114.modules.products.commons;

import com.et114.core.common.AppContext;
import com.et114.core.utility.StringPool;
import com.et114.modules.products.base.ProImage;
import com.et114.modules.products.base.ProImageServiceImpl;
import com.et114.modules.products.card.services.CardServiceImpl;
import com.et114.modules.products.cellphone.services.CellPhoneServiceImpl;
import com.et114.modules.products.digitalTv.services.DigitalTvServiceImpl;
import com.et114.modules.products.netbook.services.NetBookServiceImpl;
import com.et114.modules.products.proservices.ProService;
import com.et114.modules.products.proservices.ProServiceServiceImpl;
import com.et114.modules.products.search.SearchService;
import com.et114.modules.products.search.SearchVo;
import com.et114.modules.products.securitydoor.services.SecuritySystemServiceImpl;
import com.et114.modules.products.services.ProPropertySetImp;
import com.et114.modules.system.dictionary.DictionaryCache; 
import com.et114.modules.system.dictionary.vo.BizType;
import com.et114.modules.products.procontent.services.ProContentServiceImpl ; 
import com.et114.modules.products.procontent.vo.ProContent;
//import com.et114.components.compass.CompassIndexBuilder ;

import java.util.List; 

import nl.justobjects.pushlet.util.Log;

/*
 * @author guanhw
 */
public class Context {
	public static final String PRO_DAO_SUPPLIER_SPRING = "supplierDao" ; 
	public static final String PRO_PARAMS_SPRING_DAO = "paramsDao" ; 
	
	public static final String PRO_NETBOOK_SERVICE = "netBookServiceImpl" ;
	public static final String PRO_CELLPHONE_SERVICE = "cellPhoneServiceImpl" ;
	public static final String PRO_CARD_SERVICE = "cardServiceImpl" ;
	public static final String PRO_DIGITALTV_SERVICE = "digitalTvServiceImpl" ;
	public static final String PRO_SS_SERVICE = "securitySystemServiceImpl" ;
	
	public static final String COMMON_SLIDER_SERVICE = "sliderServiceImpl" ; 
	public static final String COMMON_NEWS_SERVICE = "commonContentImpl" ; 
	public static final String PRO_IMAGE_SERVICE = "proImageServiceImpl" ; 
	public static final String PRO_SERVICE_PLAN_SERVICE = "proServiceServiceImpl" ; 
	public static final String PRO_PROPERTY_SET_SERIVCE = "proPropertySetImp" ;
	public static final String PRO_CONTENT_SERIVCE = "proContentServiceImpl" ;
	
	public static final String PRO_COMPASS_INDEX_SERVICE_KEY = "compassIndexTask" ; 
	public static final String PRO_COMPASS_SERVICES_KEY = "searchCompass" ; 
	
	public Context ( ) {
		// TODO Auto-generated constructor stub
	}
	
	
	
	//取字典缓存
	public DictionaryCache getDictionaryCache( )  {
		return DictionaryCache.getInstance ( ) ; 
	}
	

	public List< BizType >  getCellPhonesBizTypes( ) {
		return null ; 
	}

	public static ProImageServiceImpl getProImageServiceImpl( ) {
		return ( ProImageServiceImpl )AppContext.getBean ( PRO_IMAGE_SERVICE ) ; 
	}
	
	public static ProServiceServiceImpl getProServiceServiceImpl( ) {
		return ( ProServiceServiceImpl )AppContext.getBean ( PRO_SERVICE_PLAN_SERVICE ) ; 
	}
	
	public static ProPropertySetImp getProPropertySetImp( ) {
		return ( ProPropertySetImp )AppContext.getBean ( PRO_PROPERTY_SET_SERIVCE ) ; 
	}	
	
	public static ProContentServiceImpl getProContentServiceImpl( ) {
		return ( ProContentServiceImpl  ) AppContext.getBean ( PRO_CONTENT_SERIVCE ) ;
	}
	
	public static SearchService getCompassSearchService( ) {
		return ( SearchService ) AppContext.getBean ( PRO_COMPASS_SERVICES_KEY ) ;
	}
	
	public static CellPhoneServiceImpl getCellPhoneServiceImpl( ) {
		return ( CellPhoneServiceImpl ) AppContext.getBean ( PRO_CELLPHONE_SERVICE ) ;
	}  
	
	public static NetBookServiceImpl getNetBookServiceImpl( ) {
		return ( NetBookServiceImpl ) AppContext.getBean ( PRO_NETBOOK_SERVICE ) ;
	}
	
	public static SecuritySystemServiceImpl getSecuritySystemServiceImpl( ) {
		return ( SecuritySystemServiceImpl ) AppContext.getBean ( PRO_SS_SERVICE ) ;
	} 
	
	public static CardServiceImpl getCardServiceImpl( ) {
		return ( CardServiceImpl ) AppContext.getBean ( PRO_CARD_SERVICE ) ;
	}
	
	public static DigitalTvServiceImpl getDigitalTvServiceImpl( ) {
		return ( DigitalTvServiceImpl ) AppContext.getBean ( PRO_DIGITALTV_SERVICE ) ;
	}	
	
	public static void  indexReload( ) throws Exception {
		long ct = System.currentTimeMillis ( ) ; 
		//CompassIndexBuilder index = ( CompassIndexBuilder ) AppContext.getBean ( PRO_COMPASS_INDEX_SERVICE_KEY ) ;
		//index.buildIndex ( ) ; 
		Log.info ( "Refresh Compass time:" + ( System.currentTimeMillis ( ) - ct ) + "ms"  ) ; 
	}
	
	public static void removeProRel ( Long proId , Byte proType )
			throws Exception {
		ProPropertySetImp proPropertySetImp = getProPropertySetImp ( );
		proPropertySetImp.setGlobalKey ( proType + StringPool.DASH + proId );
		proPropertySetImp.remove ( );
		
		ProImageServiceImpl imageBean = getProImageServiceImpl ( );
		ProImage image = new ProImage ( );
		image.setProType ( proType );
		image.setProId ( proId );
		imageBean.removeProImage ( image );
		
		ProServiceServiceImpl proServiceServiceImpl = getProServiceServiceImpl ( );
		ProService ps = new ProService ( proType , proId );
		proServiceServiceImpl.removeProserviceByPro ( ps );

		removeProContent( proId , proType ) ;
	}
	
	public static void removeProContent( Long proId , Byte proType ) throws Exception {
		ProContentServiceImpl proContentServiceImpl = getProContentServiceImpl ( );
		ProContent pcontent = new ProContent ( proType , proId );
		proContentServiceImpl.removeProContent ( pcontent );
	}
	
	
	public static void doCommpass4ProImage( Long proId , Byte proType  ) throws Exception {
		SearchVo searchVo = getCompassSearchService().getPro ( proId , proType ) ;
		SearchVo vo = null ; 
		if ( searchVo != null ) 
			switch( proType ) {
				case   Const.PRO_TYPE_CELLPHONE :
					vo = getCellPhoneServiceImpl( ).getCellPhone4Compass ( proId ) ; 
					if ( vo != null )
						getCompassSearchService( ).setPro ( proId , proType , vo ) ;
					
				case   Const.PRO_TYPE_CART :
					vo = getCardServiceImpl( ) .getCard4Compass ( proId ) ; 
					if ( vo != null )
						getCompassSearchService( ).setPro ( proId , proType , vo ) ;

				case   Const.PRO_TYPE_NETBOOK :
					vo = getNetBookServiceImpl( ).getNetBook4Compass (  proId ) ; 
					if ( vo != null )
						getCompassSearchService( ).setPro ( proId , proType , vo ) ;

				case   Const.PRO_TYPE_SS:
					vo = getSecuritySystemServiceImpl( ).getSecuritySystem4Compass (  proId ) ; 
					if ( vo != null )
						getCompassSearchService( ).setPro ( proId , proType , vo ) ;
					
				case   Const.PRO_TYPE_TV :
					vo = getDigitalTvServiceImpl( ).getDigitalTv4Compass (   proId ) ; 
					if ( vo != null )
						getCompassSearchService( ).setPro ( proId , proType , vo ) ;
					
			}
	}
}
